function GetQueryString(name){
    let reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r!=null){
        return decodeURI(r[2]);
    }
    return null;
}

var id = GetQueryString("id");

layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.$;
    
    window.uuid = function () { 
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function (c) {
            var r =Math.random() * 16 | 0 ,
                v = c == 'x' ? r: (r & 0x3|0x8);
            return v.toString(16);
        })
    }  
    
    $.ajax({       //初始化经销商select
        url:"/Dealer/view",
        type:"Get",
        data:"id=" + id,
        datatype:"json",
        async:true,
        success:(res)=>{
            // console.log("Debug Dealer view:",res);
            form.val("dealer_form", {
                "dealer_uid":res[0].dealer_uid,
                "dealer":res[0].dealer
                ,"shop":res[0].shop
                ,"ship_conditions":res[0].ship_conditions
                ,"order_type":res[0].order_type
                ,"remark":res[0].remark
            });
            layui.form.render("select");
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        // subdealer([data.field]);
        var confirm = layer.confirm("确认提交？", function () {
            // console.log("Debug querydata:", [data.field]);
            $.ajax({
                url: "/Dealer/update",
                type: "post",
                data: JSON.stringify([data.field]),
                datatype: "json",
                success: (res) => {
                    // layer.close(wait);
                    var alert = layer.alert(JSON.stringify(res), {
                        title: '最终的提交信息'
                    }, function () {
    
                        // 关闭alert弹出层
                        layer.close(alert);
    
                        layer.close(confirm);
    
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                }
            })
        });

        return false;
    });

});